Musiker.modBrowser = {
	name			: 	"Browse",
	artistsByName	: 	{},
	pathContent		: 	{},
	currentArtist	: 	null,
	currentAlbum	: 	null,
	currentSong		: 	null 
}

Musiker.modBrowser.Initialize = function () {
	//	Setup Browser Window --------------------------------------------------------------------------------------
	//	After fussing with this for hours on end, I finally gave up with using a pure CSS setup for this and just 
	//	used a table for the whole window.  Problem is, tables are bitch to make via the DOM, so I cheated.
	var aFrame = document.createElement('table');
		aFrame.className = 'cssAllMusicFrame';
		aFrame.innerHTML = '<tr><th>Artists</th><th>Albums</th><th>Songs</th></tr><tr><td width="30%"><div class="cssCellWrapperDiv"><div id="listArtistsFrame" class="cssLibraryList" style="border:none;"></div><div class="cssBrowserActionBar"><input type="search" value="" id="artistsSearchField"></div></div></td><td width="30%""><div class="cssCellWrapperDiv"><div id="listAlbumsFrame" class="cssLibraryList"></div><div class="cssBrowserActionBar"></div></div></td><td width="40%"><div class="cssCellWrapperDiv"><div id="listSongsFrame" class="cssLibraryList"></div><div class="cssBrowserActionBar" id="songsActionBar"></div></div></td></tr>';
		aFrame.show = MUSIKER_SHOWPANEL;
		aFrame.hide = MUSIKER_HIDEPANEL;		
		Musiker.modBrowser.Panel = aFrame
	Musiker.frames.DisplayPanel.appendChild(aFrame);
	
		var aFrame = $('listArtistsFrame');
			aFrame.setSelection = MUSIKER_BROWSER_LIST_SETSELECT;
			Musiker.modBrowser.artistListFrame = aFrame;

		var aFrame = $('listAlbumsFrame');
			aFrame.setSelection = MUSIKER_BROWSER_LIST_SETSELECT;
			Musiker.modBrowser.albumListFrame = aFrame;
		
			//add a filler frame by default
			var bFrame = document.createElement('div');
				bFrame.className = 'cssFillerFrame';
			aFrame.appendChild(bFrame);	

		var aFrame = $('listSongsFrame');
			aFrame.setSelection = MUSIKER_BROWSER_LIST_SETSELECT;
			aFrame.currentPath = null;
			Musiker.modBrowser.songListFrame = aFrame;
		
			//add a filler frame by default
			var bFrame = document.createElement('div');
				bFrame.className = 'cssFillerFrame';
			aFrame.appendChild(bFrame);
		
		
		var aFrame = $('artistsSearchField'); //setup search field
			aFrame.lastValue = "";
			aFrame.onchange = function () {
				if (this.value != this.lastValue) {
					Musiker.modBrowser.populateArtists(this.value);
					this.lastValue = this.value;
				}
			} 
			aFrame.onsearch = function () {
				if (this.value=="" && this.lastValue!="") {
					Musiker.modBrowser.populateArtists();
					this.lastValue = "";
				}
			}
			aFrame.onkeydown = function (e) { //this is here so that the spacebar and arrow keys still work
				if (!e && window.event) window.event.cancelBubble(true); //IE model
				else e.stopPropagation(true); //firefox
				return true;
			}
			Musiker.modBrowser.artistFilterField = aFrame;
	
		
		var aFrame = $('songsActionBar');
			Musiker.modBrowser.songActionBarFrame = aFrame;
		
			var aFrame = document.createElement('a');
				aFrame.className = 'cssActionBarButton';
				aFrame.innerHTML = "Queue Album"
				aFrame.onclick = MUSIKER_SONGACTIONBUTTON_QUEUEALL;
				Musiker.modBrowser.songActionBarFrame.buttonQueueAll = aFrame
			Musiker.modBrowser.songActionBarFrame.appendChild(aFrame);

			var aFrame = document.createElement('a');
				aFrame.className = 'cssActionBarButton';
				aFrame.innerHTML = "Play Album"
				aFrame.onclick = MUSIKER_SONGACTIONBUTTON_PLAYALL;
				Musiker.modBrowser.songActionBarFrame.buttonPlayAll = aFrame
			Musiker.modBrowser.songActionBarFrame.appendChild(aFrame);


	//	Setup Sidebar Row --------------------------------------------------------------------------------------
	var aFrame = document.createElement('div');
		aFrame.className = 'cssSidebar_Row';
		aFrame.innerHTML = "<img src='images/iTunesLibrary.png'><a>Browse Music</a>";
		aFrame.onclick = function () {
			Musiker.selectModule(Musiker.modBrowser);
		}
		Musiker.modBrowser.sidebarItem = aFrame;
	Musiker.frames.SidebarPanel.appendChild(aFrame);
	
}

Musiker.modBrowser.Start = function () {
	Musiker.modBrowser.populateArtists();
	Musiker.selectModule(Musiker.modBrowser);
}

Musiker.modBrowser.show = function () {
	Musiker.modBrowser.Panel.show();
	Musiker.Sidebar.setSelection(Musiker.modBrowser.sidebarItem);
}
Musiker.modBrowser.hide = function () {
	Musiker.modBrowser.Panel.hide();
}

Musiker.modBrowser.populateArtists = function (searchQuery) {
	//purge any entries existing
	while (Musiker.modBrowser.artistListFrame.childNodes.length > 0) {
		Musiker.modBrowser.artistListFrame.removeChild( Musiker.modBrowser.artistListFrame.firstChild );       
	}
	Musiker.modBrowser.artistListFrame.currentSelect = null;

	//populate the artists field
	var oddfield = 0;
	for (pName in Musiker.modBrowser.artistsByName) {
		if (!searchQuery || pName.toUpperCase().search(searchQuery.toUpperCase())>-1) {
			var fileCode = Musiker.modBrowser.artistsByName[pName];
			var aFrame = document.createElement('div');
				aFrame.className = 'cssLibraryListItem';
				
				oddfield = ++oddfield % 2;
				if (oddfield) aFrame.setAttribute('oddrow',"yes");
				
				aFrame.innerHTML = pName;
				aFrame.value = pName;
				aFrame.filePath = fileCode;
				aFrame.onclick = function () {
					if (Musiker.modBrowser.artistListFrame.currentSelection != this) { //prevent reselection
						Musiker.modBrowser.artistListFrame.setSelection(this);
						
						Musiker.modBrowser.clearList(Musiker.modBrowser.albumListFrame, true);
						Musiker.modBrowser.clearList(Musiker.modBrowser.songListFrame, true);
					
						Musiker.modBrowser.currentArtist = this.value
						
						Musiker.modBrowser.loadSubFolder(this.filePath, "albums");
					}
					return false;
				}
				aFrame.ondoubleclick = function () {return false;}
				aFrame.onmousedown = function () {return false;}
				//-----------------INSERT ARTIST LOADING CALL HERE
			Musiker.modBrowser.artistListFrame.appendChild(aFrame);
		}
	}
	
}

Musiker.modBrowser.clearList = function (listField, fill) {
	var outFrame;
	if (typeof listfield =='string') {
		if (output=='albums') outFrame = Musiker.modBrowser.albumListFrame;
		else if (output=='songs') outFrame = Musiker.modBrowser.songListFrame;
	} else outFrame = listField;
	
	
	//purge any entries existing
	while (outFrame.childNodes.length > 0) {
		outFrame.removeChild( outFrame.firstChild );       
	}
	outFrame.currentSelect = null;
	
	outFrame.currentPath = null;
	
	if (fill) {
		var aFrame = document.createElement('div');
			aFrame.className = 'cssFillerFrame';
		outFrame.appendChild(aFrame);
	}
	
	//disable action buttons if clearing the song list
	if (outFrame == Musiker.modBrowser.songListFrame) {
		Musiker.modBrowser.songActionBarFrame.buttonQueueAll.removeAttribute('enabled');
		Musiker.modBrowser.songActionBarFrame.buttonPlayAll.removeAttribute('enabled');		
	}
}

Musiker.modBrowser.loadSubFolder = function (pathString, output) {
	var outFrame;
	if (output) {
		if (output=='albums') outFrame = Musiker.modBrowser.albumListFrame;
		else if (output=='songs') outFrame = Musiker.modBrowser.songListFrame;
	}

	if (Musiker.modBrowser.pathContent[pathString]) {
		var contentList = Musiker.modBrowser.pathContent[pathString];
		
		//purge any entries existing
		Musiker.modBrowser.clearList(outFrame);

		if (output=='songs') {
			Musiker.modBrowser.songActionBarFrame.buttonQueueAll.setAttribute('enabled',true);
			Musiker.modBrowser.songActionBarFrame.buttonPlayAll.setAttribute('enabled',true);
		}
		
		outFrame.currentPath = pathString;

		//populate the list field
		var oddfield = 0;
		for (pName in contentList) {
			var fileCode = contentList[pName];
			var aFrame = document.createElement('div');
				aFrame.className = 'cssLibraryListItem';
				oddfield = ++oddfield % 2;
				if (oddfield) aFrame.setAttribute('oddrow',"yes");

			
				if (output=="songs") {
					var o = Musiker.modBrowser.parseFileName(pName);
					var nString = "";
						if (o.type) nString += "<a style='margin-right:8px;'>"+o.type.toUpperCase()+"</a>";
						if (o.disk) nString += "<a>DISC "+o.disk+"</a>";
						if (o.track) nString += "<a>TRACK "+o.track+"</a>";
					aFrame.innerHTML = nString+o.name;
					aFrame.fileName = o.name;
				} else {
					aFrame.innerHTML = pName;
					aFrame.fileName = pName;
				}
				aFrame.filePath = fileCode;
				
				aFrame.onmousedown = function () {return false;}
				aFrame.onclick = function () {
					if (outFrame.currentSelection != this) { //prevent reselection
						outFrame.setSelection(this);
					
						if (output=="albums") {
							Musiker.modBrowser.loadSubFolder(this.filePath, "songs");
							Musiker.modBrowser.currentAlbum = this.fileName;
						} else if (output=="songs") {
							Musiker.modBrowser.currentSong = this.fileName;
						}
						
					}
					return false;
				}
				aFrame.oncontextmenu = function () {
					return false;
				}
				aFrame.ondblclick = function () {
					if (output=="songs") {
						if (Musiker.modHardPlaylists) Musiker.modHardPlaylists.lockedPlaylist = null;
						MediaPlayer.appendAndPlay(
							Base64.decode(this.filePath).replace('\+','\%2B'), //the replacement is because mediaplayer thinks + is a space and requests the wrong file
							this.fileName,
							Musiker.modBrowser.currentArtist,
							this.filePath,
							Musiker.modBrowser.currentSong
						);
					}
					return false;
				}
			outFrame.appendChild(aFrame);
		}
	} else {		
		if (output) {
			var aFrame = document.createElement('div');
				aFrame.className = 'cssWaitFrame';
			outFrame.appendChild(aFrame);
		}

		SimpleFetch.FetchAndEval('FetchPathData.js.php?path='+pathString+'&output='+output);
	}
}

Musiker.modBrowser.parseFileName = function (nameString) {
	var nameObj = {name:nameString};
	
	//match for disk+track file name
	var re = /^(\d)-(\d+) (.+)\.(.+)$/
	var ma = re.exec(nameString);
	if (ma) {
		nameObj['disk'] = ma[1];
		nameObj['track'] = ma[2];
		nameObj['name'] = ma[3];
		nameObj['type'] = ma[4];
		return nameObj;
	}

	//match for disk+track file name
	var re = /^(\d+) (.+)\.(.+)$/
	var ma = re.exec(nameString);
	if (ma) {
		nameObj['track'] = ma[1];
		nameObj['name'] = ma[2];
		nameObj['type'] = ma[3];
		return nameObj;
	}
	
	//match for just file name
	var re = /^(.+)\.(.+)$/
	var ma = re.exec(nameString);
	if (ma) {
		nameObj['name'] = ma[1];
		nameObj['type'] = ma[2];
		return nameObj;
	}
	
	//still didn't match, just return what was sent
}

Musiker.modBrowser.addAlbumToPlaylist = function (pathString, play) {
	if (pathString) {
		Musiker.modHardPlaylists.lockedPlaylist = null;
		
		var startIndex = MediaPlayer.playlistLength();
		if (MediaPlayer.defaultState()) startIndex -= 1;
		var pContents = Musiker.modBrowser.pathContent[pathString];
		
		for (pName in pContents) {
			
			var fileCode = pContents[pName];
			var o = Musiker.modBrowser.parseFileName(pName);
					
			MediaPlayer.appendToPlaylist(
				Base64.decode(fileCode).replace('\+','\%2B'), //the replacement is because mediaplayer thinks + is a space and requests the wrong file
				o.name,
				Musiker.modBrowser.currentArtist,
				fileCode,
				Musiker.modBrowser.currentAlbum
			);
		}
		
		if (play) MediaPlayer.playTrack(startIndex);
	}
}

MUSIKER_BROWSER_LIST_SETSELECT = function (element) {
	if (this.currentSelection) this.currentSelection.removeAttribute("activeSelect");
	this.currentSelection = element;
	element.setAttribute("activeSelect","yes");
}

MUSIKER_SONGACTIONBUTTON_QUEUEALL = function () {
	Musiker.modBrowser.addAlbumToPlaylist(Musiker.modBrowser.songListFrame.currentPath);
}

MUSIKER_SONGACTIONBUTTON_PLAYALL = function () {
	Musiker.modBrowser.addAlbumToPlaylist(Musiker.modBrowser.songListFrame.currentPath, true);
}

//insert the script tag to load the artists list
var aFrame = document.createElement('script');
	aFrame.setAttribute('src', "FetchArtistList.js.php");
	document.body.appendChild(aFrame);


Musiker.RegisterModule(Musiker.modBrowser);